<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员欢迎语
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
			array('op'=>'list','title'=>'欢迎语列表'),
			array('op'=>'edit','title'=>'添加欢迎语'),
);
$return['types']=array(
	'0'=>'文本',
	'1'=>'图片',
	'2'=>'语音（音乐）',
	'3'=>'视频',
	'4'=>'图文',
);
//会员欢迎语
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_qun_welcome',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['usernum']=pdo_count('jy_scrm_member',array('groupid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['qunnum']=0;
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'
			1、这里是通过API向企业的入群欢迎语素材库中添加素材。每个企业的入群欢迎语素材库中，最多容纳100个素材。
			</br>2、添加后，群主管理员，可以再企业微信APP端，进入群，设置群欢迎语。
			</br>3、第三方应用需具有“企业客户权限->客户联系->配置入群欢迎语素材”权限
		','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'type','title'=>'欢迎语类型','type'=>'radio','options'=>'types'),
			//array('name'=>'content','title'=>'欢迎语内容','type'=>'string'),
			array('name'=>'bumenid','title'=>'所属部门','type'=>'select','options'=>'groups','option_value'=>'bumenid'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'batch'=>array(
			//array('type'=>'post','url'=>'jy_scrm/member.qunwelcome/upqun/','title'=>'同步素材'),
		)),
		'search'=>array(
			array('name'=>'type','width'=>15,'title'=>'欢迎语类型','type'=>'radio','options'=>'types'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['groups']=$this->getall('worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'),'bumenid');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_qun_welcome',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('type'=>0);
	}
	$return['groups']=$this->getall('worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'));
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写欢迎语名称',true);
	}
	if(!$postdata['content']){
		exi('请填写欢迎语内容',true);
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'type'=>intval($postdata['type']),
		'bumenid'=>intval($postdata['bumenid']),
		'content'=>$postdata['content'],
		'status'=>1,
	);
	$id=intval($postdata['id']);
	$template_id=$this->model('model')->upqun_welcome($id,$data);
	if($id){
		if(!$postdata['template_id']){
			$data['template_id']=$template_id;
		}
		pdo_update('jy_scrm_qun_welcome',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['template_id']=$template_id;
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_scrm_qun_welcome',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择用户组！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_scrm_qun_welcome',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_qun_welcome',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}